package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.controller.dto.LoginDTO;
import com.example.springboot.controller.request.LoginRequest;
import com.example.springboot.controller.request.AdminPageRequest;
import com.example.springboot.controller.request.PasswordRequest;
import com.example.springboot.entity.Admin;
import com.example.springboot.entity.Room;
import com.example.springboot.service.IAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    IAdminService adminService;

    @PutMapping("/login")
    public Result login(@RequestBody LoginRequest request) {
        LoginDTO login = adminService.login(request);
        return Result.success(login);
    }
//    登录的mapping请求一定要放在最上面，血的教训

    @PutMapping("/password")
    public Result password(@RequestBody PasswordRequest request) {
        adminService.changePass(request);
        return Result.success();
    }

    @GetMapping("/list")
    public Result list() {
        List<Admin> list = adminService.list();
        return Result.success(list);
    }

    @GetMapping("/{username}")
    public Result getById(@PathVariable String username) {
        Admin admin = adminService.getById(username);
        return (Result) Result.success(admin);

    }

    @GetMapping("/page")
    public Result page(AdminPageRequest adminPageRequest) {
        return (Result) Result.success(adminService.page(adminPageRequest));

    }

    @PutMapping("/update")
    public Result update(@RequestBody Admin admin) {
        adminService.update(admin);
        return Result.success();
    }

    @PostMapping("/add")
    public Result add(@RequestBody Admin admin) {
        adminService.add(admin);
        return Result.success();
    }



    @PostMapping ("/delete")
    public Result delete(@RequestBody Admin admin) {
        adminService.delete(admin);
        return Result.success();
    }
//    @PutMapping("/login")
//    public Result login(@RequestBody LoginRequest request) {
//        LoginDTO login = adminService.login(request);
//        return Result.success(login);
//    }


}
